package com.hpeu_01.day0125;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 使用URL编程来获取网络资源
 */
public class URLDemo02 {
    public static void main(String[] args) throws Exception {
        // 1. 创建URL对象
        URL url = new URL("http://pic1.win4000.com/wallpaper/2019-10-08/5d9c396c819d2.jpg");
        // 2. 创建连接对象
        URLConnection urlConnection = url.openConnection();
        // 3. 创建HTTPURLConnection
        HttpURLConnection conn = (HttpURLConnection) urlConnection;

        // 4. 创建输入流
        InputStream is = conn.getInputStream();

        // 5. 创建输出流
        FileOutputStream fos = new FileOutputStream("D:\\project\\JAVA\\java\\JavaSE\\src\\com\\hpeu_01\\day0125\\美女.jpg");

        byte[] b = new byte[1024];
        int len = -1;
        while ((len = is.read(b)) != -1){
            fos.write(b,0,len);
        }

        fos.close();
        is.close();
        conn.disconnect();
    }
}
